package com.ziyucoding.springboot.labs.lab4.event.listner;

import com.ziyucoding.springboot.labs.lab4.event.events.OrderProductEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 订单商品监听器
 * 监听并处理事件，实现 ApplicationListener 接口或者使用 @EventListener
 *
 * @author ziyucoding
 * @date 2022-10-25
 **/

@Slf4j
@Component
public class OrderProductListener implements ApplicationListener<OrderProductEvent> {

    /**
     * 使用 onApplicationEvent 方法对消息进行接收处理
     *
     * @param event
     * @return void
     **/
    @Override
    public void onApplicationEvent(OrderProductEvent event) {
        String orderId = event.getOrderId();
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            log.info("线程休眠异常", e);
        }
        long end = System.currentTimeMillis();
        log.info("{}：校验订单商品价格耗时：({})毫秒", orderId, (end - start));
    }
}
